package com.propertypoint.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;







import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.propertypoint.presentation.model.Property;

public class PropertyFeedUtil {
	
	private static volatile PropertyFeedUtil instance = null;
	  private PropertyFeedUtil() {
	    }
	 
	    public static PropertyFeedUtil getInstance() {
	        if (instance == null) {
	            synchronized (PropertyFeedUtil.class) {
	               
	                if (instance == null) {
	                    instance = new PropertyFeedUtil();
	                }
	            }
	        }
	        return instance;
	    }
	
	private String constructUrl(Map<String, String[]> parameterMap, String url) {

		if (MapUtils.isNotEmpty(parameterMap) && StringUtils.isNotEmpty(url)) {

			for (String param : parameterMap.keySet()) {
				String[] paramValues = parameterMap.get(param);
				if (ArrayUtils.isNotEmpty(paramValues)
						&& paramValues.length == 1) {
					url=url.concat("&").concat(param).concat("=")
							.concat(paramValues[0]);

				}

			}

		}

		return url;

	}
	
	@Deprecated
	public  List<Property> processSTPropertyFeed(Map<String, String[]> parameterMap, String url) throws IOException{
		
		URL pipeUrl = new URL(constructUrl(parameterMap,url));  
		List<Property> propertyList = new ArrayList<Property>();
	
		InputStreamReader in = new InputStreamReader(pipeUrl.openStream());
		BufferedReader buf = new BufferedReader(in);
		String inputLine;
		StringBuffer buffOut = new StringBuffer();
		while ((inputLine = buf.readLine()) != null)
			buffOut.append(inputLine);
		JsonObject jsonObject = new JsonObject();		
		JsonParser paser = new JsonParser();
		JsonElement element=paser.parse( buffOut.toString());
		jsonObject=element.getAsJsonObject();
		element = jsonObject.get("value");
		jsonObject =element.getAsJsonObject();
		element = jsonObject.get("items");
		JsonArray arry  = element.getAsJsonArray();
		
		Type listType = new TypeToken<List<Property>>() {}.getType();
		
		propertyList = new Gson().fromJson(arry.toString(), listType);
		
		return propertyList;
		
	}
	
	public  JsonObject callFeed(Map<String, String[]> parameterMap, String url) throws IOException{
		
		URL pipeUrl = new URL(constructUrl(parameterMap,url)); 		
	
		InputStreamReader in = new InputStreamReader(pipeUrl.openStream());
		BufferedReader buf = new BufferedReader(in);
		String inputLine;
		StringBuffer buffOut = new StringBuffer();
		while ((inputLine = buf.readLine()) != null)
			buffOut.append(inputLine);
		JsonObject jsonObject = new JsonObject();		
		JsonParser paser = new JsonParser();
		JsonElement element=paser.parse( buffOut.toString());
		jsonObject=element.getAsJsonObject();
		
		
		return jsonObject;
		
	}

}
